package com.hm.hall.commons.util;

import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * 随机串生成算法
 *
 * @author wei
 */
public class RandomUtil {
    /**
     * 随机的6位数字 字符串 --短信验证码
     *
     * @return
     */
    public static String randomSixFiguresDigits() {
        int min = 100000;
        int max = 999999;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return String.valueOf(tmp % (max - min + 1) + min);
    }

    public static void main(String[] args) {
        System.out.println(randomSixFiguresDigits());
        System.out.println(randomEightFiguresDigits());
        short a = 1;
        short b = 2;
        System.out.println(a | b);
    }


    /**
     * 随机的8位数字 字符串  --
     *
     * @return
     */
    public static String randomEightFiguresDigits() {
        int min = 10000000;
        int max = 99999999;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return String.valueOf(tmp % (max - min + 1) + min);
    }

    /**
     * 随机的1位数字
     *
     * @return
     */
    public static int randomOneFiguresDigits() {
        int min = 1;
        int max = 9;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (max - min + 1) + min;
    }

    /**
     * 随机的2位数字  --用于群id生成的步长  step
     *
     * @return
     */
    public static int randomTwoFiguresDigits() {
        int min = 10;
        int max = 99;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (max - min + 1) + min;
    }

    /**
     * 随机的5位数字  --用于推广ID生成
     *
     * @return
     */
    public static int randomFiveFiguresDigits() {
        int min = 10000;
        int max = 99999;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (max - min + 1) + min;
    }

    /**
     * 随机16位
     */
    @Deprecated
    public static String randomSixteenFiguresDigits() {

        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        str = str.toUpperCase();
        str = str.replaceAll("-", "");
        String resp = str.substring(0, 16);
        return resp;

    }

    /**
     * 随机的4位数字 字符串 --短信验证码
     *
     * @return
     */
    public static String randomFourFiguresDigits() {
        int min = 1000;
        int max = 9999;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return String.valueOf(tmp % (max - min + 1) + min);
    }

    /**
     * 随机的4位数字 字符串 --兼职总数
     *
     * @return
     */
    public static String randomPartTimeCountDigits() {
        int min = 1000;
        int max = 2000;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return String.valueOf(tmp % (max - min + 1) + min);
    }

    /**
     * 随机的4位数字 字符串 --全职总数
     *
     * @return
     */
    public static String randomFullTimeCountDigits() {
        int min = 4000;
        int max = 5000;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return String.valueOf(tmp % (max - min + 1) + min);
    }

    /**
     * 随机的8位数字 字符串 --全职总数
     *
     * @return
     */
    public static String randomEightCountDigits() {
        int min = 10000000;
        int max = 99999999;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return String.valueOf(tmp % (max - min + 1) + min);
    }

    /**
     * 随机生成手机号码
     *
     * @return
     */
    public static String randomMobile() {

		 /*133 、 153 、 1700 、 177 、 180 、 181 、 189 号段数据
		 移动号码匹配 134 、 135 、 136 、 137 、 138 、 139 、 150 、 151 、 152 、 157 、 158 、 159 、 1705 、
		 178 、 182 、 183 、 184 、 187 、 188 、 147 号段数据
		 联通号码匹配 130 、 131 、 132 、 155 、 156 、 1709 、 176 、 185 、 186 、 145*/
        String[] prifixMobile = {"133", "153", "180", "181", "189", "134", "135", "136", "137", "138", "139", "150", "151", "152", "157"
                , "158", "159", "178", "182", "183", "184", "187", "188", "147", "130", "131", "132", "155", "156", "176", "185", "186", "145"};
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        int prifixNum = tmp % (prifixMobile.length - 1) + 1;
        return prifixMobile[prifixNum] + randomEightCountDigits();
    }

    /**
     * 随机生成QQ号
     *
     * @return
     */
    public static String randomQQ() {
        int min = 100000000;
        int max = 999999999;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return String.valueOf(tmp % (max - min + 1) + min);
    }

    /**
     * 随机生成userId
     *
     * @return
     */
    public static String randomHideUserId() {
        int min = 00;
        int max = 99;
        String prefixUserId;
        String suffixUserId;
        if (randomBoolean()) {
            prefixUserId = "24";
        } else if (randomBoolean()) {
            prefixUserId = "25";
        } else {
            prefixUserId = "19";
        }
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        suffixUserId = String.valueOf(tmp % (max - min + 1) + min);
        if (suffixUserId.length() < 2) {
            suffixUserId = 0 + suffixUserId;
        }

        return prefixUserId + "XXX" + suffixUserId;
    }

    /**
     * 随机生成Boolean
     *
     * @return
     */
    public static Boolean randomBoolean() {
        int min = 0;
        int max = 1;

        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        if (1 == (tmp % (max - min + 1) + min)) {
            return true;
        } else {
            return false;
        }
    }

    public static int randomByMinAndMax(int min, int max) {
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return (tmp % (max - min + 1) + min);
    }

    public static long randomByMinAndMax(long min, long max) {
        Random rand = new Random();
        long tmp = Math.abs(rand.nextLong());
        return (tmp % (max - min + 1) + min);
    }


    /**
     * 概率获得值
     * 参数 map 例:
     * {
     * "0":"0.5", //50% 获得0
     * "1":"0.2", //20% 获得1
     * "2":"0.3",//30% 获得2
     * }
     *
     * @return key 如果不存在返回-1
     */
    public static String percentageRandom(Map<String, Object> map) {
        double randomNumber = Math.random();
        double minRate = 0.0, maxRate = 0.0;

        for (String key : map.keySet()) {
            minRate = maxRate;
            maxRate = maxRate + Double.valueOf("" + map.get(key));
            if (randomNumber >= minRate && randomNumber < maxRate) {
                return key;
            }
        }
        return "-1";
    }


}

